package com.ytjj.qmyx.mall.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.qmyx.mall.model.response.ScoreHomeResponse;
import com.ytjj.qmyx.mall.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 品牌
 *
 *
 * @version 1.0
 *
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;
    @Resource
    private RedisService redisService;
    @Resource
    private SysConfigUtil sysConfigUtil;
    /**
     * 获取品牌列表
     *
     * @return
     */
    @GetMapping("/getBrandList")
    public CommonResult getBrandList() {
        return CommonResult.success(brandService.getBrandList());
    }

    /**
     * 获取品牌下商品(积分商品)
     *
     * @return
     */
    @GetMapping("/getBrandProductList")
    public CommonResult getBrandProductList(@RequestParam Integer brandId) {
        return CommonResult.success(brandService.getBrandProductList(brandId));
    }


    /**
     * @Description 获取积分首页
     *
     *
     *
     * @Version 1.0
     **/
    @GetMapping("/getScoreHome")
    public CommonResult getScoreHome() {
        UsersResponse h5CurrentUser = getH5CurrentUser();
        ScoreHomeResponse scoreHome = null;
        String s = redisService.get(com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + "SCORE_HOME_V2") ;
        if (StringUtils.isNotEmpty(s)) {
            scoreHome = JSONObject.parseObject(s, ScoreHomeResponse.class);
        } else {
            scoreHome = brandService.getScoreHome(h5CurrentUser);
            redisService.set(com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + "SCORE_HOME_V2", JSONObject.toJSONString(scoreHome));
            redisService.expire(com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + "SCORE_HOME_V2", Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.REDIS_TIME)));
        }
        return CommonResult.success(scoreHome);
    }
}
